package com.easycarebn.main.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.easycarebn.request.data.DataItem;
import com.timbacsi.main.MainActivity;
import com.timbacsi.main.R;

public class AutoCompleteAdapter extends ArrayAdapter<DataItem> {

	final String TAG = "AutocompleteCustomArrayAdapter.java";

	Context mContext;
	int layoutResourceId;

	public AutoCompleteAdapter(Context mContext, int layoutResourceId, ArrayList<DataItem> data) {
		super(mContext, layoutResourceId, data);

		this.layoutResourceId = layoutResourceId;
		this.mContext = mContext;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		try {

			/*
			 * The convertView argument is essentially a "ScrapView" as
			 * described is Lucas post
			 * http://lucasr.org/2012/04/05/performance-tips
			 * -for-androids-listview/ It will have a non-null value when
			 * ListView is asking you recycle the row layout. So, when
			 * convertView is not null, you should simply update its contents
			 * instead of inflating a new row layout.
			 */
			if (convertView == null) {
				// inflate the layout
				LayoutInflater inflater = ((MainActivity) mContext).getLayoutInflater();
				convertView = inflater.inflate(layoutResourceId, parent, false);
			}
			ViewHolder holder = (ViewHolder) convertView.getTag();
			if(holder == null) {
			    holder = new ViewHolder();
			    holder.mTvName = (TextView) convertView.findViewById(R.id.textViewItem);
                convertView.setTag(holder);
			}
			// object item based on the position
			DataItem objectItem = getItem(position);

			// get the TextView and then set the text (item name) and tag (item
			// ID) values
			holder.mTvName.setText(objectItem.getName());

		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;

	}
	
	private static class ViewHolder {
	    TextView mTvName;
	}
}